$(document).ready(function(){
	 $('.table-sort').dataTable(pageStting());
	 $('#DataTables_Table_0_length').find('label').contents().wrap("<span class='f-l mr-10'></span>");
});
$(window).resize(function(){
	$(".table-sort").css('width','100%');
});
//从服务器分批取数据
pageStting = function(){
		var optsortable = {
					"bPaginate":true,
					"stateSave":true,
					"bProcessing":true,
					"iDisplayLength":10,
					"sAjaxSource":baseURL+'/api/audit/list',
					"sPaginationType":'full_numbers',
					"bServerSide": true,
					"bSort": false,             
					"bFilter": false,  
				     "aoColumnDefs": [
				                      {
				                    	"bVisible":false,
				                    	"targets":0
				                      }
				                      ] ,
					"fnServerData": function(sSource, aoData, fnCallback){
							var globalSearch = {};
							var searchConditions = {};
							if($.trim($("#stratDate").val())!=''){
								searchConditions.beginDate=$.trim($("#stratDate").val());
							}
							if($.trim($("#endDate").val())!=''){
								searchConditions.endDate=$.trim($("#endDate").val());
							}
							if($.trim($("#userName").val())!=''){
								searchConditions.userId=$.trim($("#userName").val());
							}
							globalSearch.name= 'globalSearch';
							globalSearch.value= JSON.stringify(searchConditions);
							aoData.push(globalSearch);
							 $.ajax({  
						        type: "GET",     
						        url: sSource,     
						        dataType: "json",    
						        contentType: "application/json;charset=UTF-8", 
						        data: aoData, // 以json格式传递  
						        success: function(result) {
						        	if(!result.flag){
					            		if(result.statusCode==503){
					            		  location=baseURL+"/api/tologin";
					            		}else{
					            		  layer.msg("查询失败！");
					            		}
					            	  return;
					            	}
						        	if(result.flag){
							        	var obj =result;
										var data = obj.results;
										var aaData = [];
										var dataResult = {};
										dataResult.iTotalRecords = obj.totalcount;
										dataResult.iTotalDisplayRecords= obj.totalcount;
										dataResult.iDisplayStart = obj.offset;
										dataResult.iDisplayLength= obj.limit;
										dataResult.aaData = aaData;
										//json重构数组对象 
										$.each(data,function(i,val){
											aaData[i]=[]
											aaData[i].push(val.id);
											aaData[i].push(val.userName);
											aaData[i].push(getTime(val.createDate));
											aaData[i].push(val.channel);
											aaData[i].push(getContent(val.messageTemplate==null?"-":val.messageTemplate));
										});
							            fnCallback(dataResult);   
						          }
						        }    
						     });
					}
				}	
		return optsortable;
}
doSearch = function(){
   var seachValue={};
   $('.table-sort').DataTable().search(seachValue).draw();
}	
doReset= function(){
	$('.searchCommon').find('input,select').val('');
}
function getTime(t){
	 var date=new Date(t);
	 return date.getFullYear()+"-"+changeTime((date.getMonth()+1))+"-"+changeTime(date.getDate())+"&nbsp;"+changeTime(date.getHours())+":"+changeTime(date.getMinutes());
}
function changeTime(obj){
return obj<10?"0"+obj:obj;
}
function getContent(obj){
	if(obj.length<=80){
		return "<span>"+obj+"</span>";
	}else return "<span>"+obj.substring(0,80)+"...</span>";
}